﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CampaignBehaviors.CampaignBehaviorManager
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.CampaignBehaviors
{
  public class CampaignBehaviorManager : ICampaignBehaviorManager
  {
    private List<CampaignBehaviorBase> _campaignBehaviors;
    [SaveableField(1)]
    private readonly CampaignBehaviorDataStore _campaignBehaviorDataStore;

    public CampaignBehaviorManager(IEnumerable<CampaignBehaviorBase> inputComponents)
    {
      this.SetBehaviors(inputComponents);
      this._campaignBehaviorDataStore = new CampaignBehaviorDataStore();
      CampaignEvents.OnBeforeSaveEvent.AddNonSerializedListener((object) this, new Action(this.OnBeforeSave));
    }

    public void InitializeCampaignBehaviors(IEnumerable<CampaignBehaviorBase> inputComponents)
    {
      this.SetBehaviors(inputComponents);
      CampaignEvents.OnBeforeSaveEvent.AddNonSerializedListener((object) this, new Action(this.OnBeforeSave));
    }

    private void SetBehaviors(IEnumerable<CampaignBehaviorBase> inputComponents)
    {
      this._campaignBehaviors = inputComponents.ToList<CampaignBehaviorBase>();
    }

    public void RegisterEvents()
    {
      foreach (CampaignBehaviorBase campaignBehavior in this._campaignBehaviors)
        campaignBehavior.RegisterEvents();
    }

    private void OnBeforeSave()
    {
      this._campaignBehaviorDataStore.ClearBehaviorData();
      foreach (CampaignBehaviorBase campaignBehavior in this._campaignBehaviors)
        this._campaignBehaviorDataStore.SaveBehaviorData(campaignBehavior);
    }

    public void LoadBehaviorData()
    {
      foreach (CampaignBehaviorBase campaignBehavior in this._campaignBehaviors)
        this._campaignBehaviorDataStore.LoadBehaviorData(campaignBehavior);
      this._campaignBehaviorDataStore.ClearBehaviorData();
    }

    public T GetBehavior<T>() => this._campaignBehaviors.OfType<T>().FirstOrDefault<T>();

    public IEnumerable<T> GetBehaviors<T>() => this._campaignBehaviors.OfType<T>();

    public void AddBehavior(CampaignBehaviorBase campaignBehavior)
    {
      this._campaignBehaviors.Add(campaignBehavior);
      campaignBehavior.RegisterEvents();
    }

    public void RemoveBehavior<T>() where T : CampaignBehaviorBase
    {
      for (int index = this._campaignBehaviors.Count - 1; index >= 0; --index)
      {
        if (this._campaignBehaviors[index] is T campaignBehavior)
        {
          this._campaignBehaviors.Remove((CampaignBehaviorBase) campaignBehavior);
          CampaignEventDispatcher.Instance.RemoveListeners((object) campaignBehavior);
          break;
        }
      }
    }

    public void ClearBehaviors() => this._campaignBehaviors.Clear();

    internal static void AutoGeneratedStaticCollectObjectsCampaignBehaviorManager(
      object o,
      List<object> collectedObjects)
    {
      ((CampaignBehaviorManager) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._campaignBehaviorDataStore);
    }

    internal static object AutoGeneratedGetMemberValue_campaignBehaviorDataStore(object o)
    {
      return (object) ((CampaignBehaviorManager) o)._campaignBehaviorDataStore;
    }
  }
}
